package com.whyalwaysmea.main;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import com.whyalwaysmea.mapper.RoleMapper;
import com.whyalwaysmea.mapper.RoleMapper2;
import com.whyalwaysmea.pojo.Role;
import com.whyalwaysmea.utils.SqlSessionFactoryUtils;

public class MainTest {
	public static void main(String[] args) {
		SqlSession sqlSession = SqlSessionFactoryUtils.openSqlSession();
		RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
		
		// 新增测试
		/*Role zhaoxin = new Role("德邦总管", "人之死，难免也。可免者，唯败也。");
		int insertRole = roleMapper.insertRole(zhaoxin); */
		
		// 删除测试
		// int deleteRole = roleMapper.deleteRole(id);
		
		// 更新测试
		// Role vn = new Role("暗夜猎手", "罪恶潜伏在各个角落");
		// vn.setId(16L);
		// int updateRole = roleMapper.updateRole(vn);
		
		// 模糊查询测试
		List<Role> findRoles = roleMapper.findRoles("德");
		for (Role role : findRoles) {
			System.out.println(role);
		}
		
		// 因为开启了事务，所以需要手动提交
		sqlSession.commit();
	}

	/**
	 * 使用SqlSession发送sql
	 */
	public static void sqlSessionSQL() {
		SqlSession session = SqlSessionFactoryUtils.openSqlSession();
		// selectOne表示查询并且只返回一个对象，参数是一个String对象和一个Object对象  
		// String对象是由一个命名空间加上SQL id组合而成的。
		// Role role = session.selectOne("com.whyalwaysmea.mapper.RoleMapper.getRole", 1L);
		// 如果在MyBatis中只有一个id为getRole的SQL，那么可以简写
		Role role = session.selectOne("getRole", 1L);
		System.out.println(role.toString());
	}
	
	/**
	 * 使用Mapper发送sql
	 * Mapper由xml配置
	 */
	public static void mapperSQL() {
		SqlSession session = SqlSessionFactoryUtils.openSqlSession();
		RoleMapper roleMapper = session.getMapper(RoleMapper.class);
		int deleteRole = roleMapper.deleteRole(1L);
		System.out.println(deleteRole);
	}
	
	/**
	 * 使用Mapper发送sql
	 * Mapper由注解配置
	 */
	public static void mapper2SQL() {
		SqlSession session = SqlSessionFactoryUtils.openSqlSession();
		RoleMapper2 roleMapper = session.getMapper(RoleMapper2.class);
		Role role = roleMapper.getRole(1L);
		System.out.println(role);
	}
}
